package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Result;
import com.example.demo.entity.Article;
import com.example.demo.mapper.ArticleMapper;
import com.example.demo.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "文章访问接口", value = "文章控制端")
@RestController
@CrossOrigin
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    private ArticleMapper articlemapper;
    @Autowired
    private UserMapper usermapper;

    @ApiOperation(value = "获取所有文章信息")
    @GetMapping("/info")
    public Result<?> selectAll(){
        List list = articlemapper.selectList(null);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到文章信息！");
        }
    }

    @ApiOperation(value = "根据文章id获取文章信息")
    @GetMapping("/info/id")
    public Result<?> selectById(@RequestParam int id)
    {
        if(String.valueOf(id).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        Article article = articlemapper.selectById(id);
        if(article != null) {
            return Result.success(article);
        } else {
            return Result.error("404", "找不到文章信息！");
        }
    }

    @ApiOperation(value = "搜索文章内容获取文章信息")
    @GetMapping("/info/search")
    public Result<?> search(@RequestParam String str){
        //System.out.println(str);
        LambdaQueryWrapper<Article> wrapper = Wrappers.<Article>lambdaQuery();
        wrapper.like(Article::getCont, str);
        List list = articlemapper.selectList(wrapper);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到文章信息！");
        }
    }

    @ApiOperation(value = "根据作者id获取文章信息")
    @GetMapping("/info/auid")
    public Result<?> selectByAuid(@RequestParam int auid)
    {
        if(String.valueOf(auid).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        List list = articlemapper.selectList(Wrappers.<Article>lambdaQuery().eq(Article::getAuid, auid));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到文章信息！");
        }
    }

    @ApiOperation(value = "根据文章类型获取文章信息")
    @GetMapping("/info/type")
    public Result<?> selectByType(@RequestParam int type)
    {
        if(String.valueOf(type).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        List list = articlemapper.selectList(Wrappers.<Article>lambdaQuery().eq(Article::getType, type));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到文章信息！");
        }
    }

    @ApiOperation(value = "获取待审核文章信息")
    @GetMapping("/info/stat")
    public Result<?> selectStatByPage(@RequestParam(defaultValue = "1") int num,
                                      @RequestParam(defaultValue = "10") int size,
                                      @RequestParam(defaultValue = "") String str)
    {
        LambdaQueryWrapper<Article> wrapper = Wrappers.<Article>lambdaQuery();
        // 建立多条件查询器
        if (!Objects.equals(str, "")){
            // 是否包含搜索内容
            wrapper.eq(Article::getStat, 0)
                    // 待审，下同
                    .and(qw->qw.like(Article::getTitle, str).or().like(Article::getCont, str));
            // 标题或内容含有str，下同
            // 设置查询器条件符合上述条件的文章信息，下同
        } else {
            wrapper.eq(Article::getStat, 0);
        }

        Page page = articlemapper.selectPage(new Page<>(num, size), wrapper);
        // 按wrapper查询以每页size条数据为大小的第num页数据
        if(!page.getRecords().isEmpty()) {
            // 是否查询成功
            return Result.success(page);
            // 返回成功信息
        } else {
            return Result.error("404", "找不到文章信息！");
            // 返回失败信息
        }
    }

    @ApiOperation(value = "按页数获取文章页面")
    @GetMapping("/info/page")
    // 后端请求地址
    public Result<?> selectByPage(@RequestParam(defaultValue = "1") int num,
                                  @RequestParam(defaultValue = "10") int size,
                                  @RequestParam(defaultValue = "") String str, int type){
        LambdaQueryWrapper<Article> wrapper = Wrappers.<Article>lambdaQuery();
        // 建立多条件查询器
        if (!Objects.equals(str, "")){
            // 是否包含搜索内容
            if(type == 0) {
                // 是否搜索特定文章类型
                wrapper.eq(Article::getStat, 1)
                        // 过审，下同
                        .orderByDesc(Article::getTime)
                        // 按发布时间降序排列，下同
                        .and(qw->qw.like(Article::getTitle, str).or().like(Article::getCont, str));
                        // 标题或内容含有str，下同
                // 设置查询器条件符合上述条件的文章信息，下同
            } else {
                wrapper.eq(Article::getType, type)
                        // 文章类型为type
                        .eq(Article::getStat, 1).orderByDesc(Article::getTime)
                        .and(qw->qw.like(Article::getTitle, str).or().like(Article::getCont, str));
            }
        } else {
            if(type == 0) {
                wrapper.eq(Article::getStat, 1);
            } else {
                wrapper.eq(Article::getType, type).eq(Article::getStat, 1).orderByDesc(Article::getTime);
            }
        }

        Page page = articlemapper.selectPage(new Page<>(num, size), wrapper);
        // 按wrapper查询以每页size条数据为大小的第num页数据
        if(!page.getRecords().isEmpty()) {
            // 是否查询成功
            return Result.success(page);
            // 返回成功信息
        } else {
            return Result.error("404", "找不到文章信息！");
            // 返回失败信息
        }
    }

    @ApiOperation(value = "添加文章信息")
    @PostMapping("")
    public Result<?> insert(Article article) {
        if(usermapper.selectById(article.getAuid()) == null) {
            return Result.error("404", "文章的作者id不存在！");
        }
        article.setStat(0);
        boolean bool = (articlemapper.insert(article) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "添加文章失败！");
        }
    }

    @ApiOperation(value = "根据文章id删除文章信息")
    @DeleteMapping("")
    public Result<?> delete(@RequestParam int id){
        if(String.valueOf(id).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        boolean bool = (articlemapper.deleteById(id) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "删除文章失败！");
        }
    }

    @ApiOperation(value = "根据文章id更新文章信息")
    @PutMapping("")
    public Result<?> update(Article article){
        if(usermapper.selectById(article.getAuid()) == null) {
            return Result.error("404", "文章的作者id不存在！");
        }
        if(article.getArid() == 0) {
            return Result.error("401", "参数不能为空！");
        }
        boolean bool = (articlemapper.updateById(article) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "更新文章失败！");
        }
    }
}